<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>axios 调用方法发送请求</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.1.2/axios.js"></script>
  </head>
  <body>
    <button>get</button>
    <button>post</button>

    <script>
      //获取所有的按钮
      let btns = document.querySelectorAll("button");
      //绑定事件
      btns[0].onclick = async function () {
        try {
          //url 是必填项
          // axios.get(url[, config]);    config 是配置对象, 设置请求相关的内容
          let response = await axios.get("http://127.0.0.1:3000/book", {
            params: {
                q: '三'
            }
          });
          console.log(response);
        } catch (e) {
          console.log(e);
        }
      };

      //发送 POST 请求
      btns[1].onclick = async function () {
        try {
          // url 是必填项
          // axios.post(url[, data[, config]])    
          let response = await axios.post("http://127.0.0.1:3000/book", {title: '不能再睡啦~~', price: 19.9});
          console.log(response);
        } catch (e) {
          console.log(e);
        }
      };
      

    </script>
  </body>
</html>
